package com.example.redcultural;

import android.content.Context;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class NewsGet {

    private String back="";

    public String get(final String type) {
        final String urllink = "http://v.juhe.cn/toutiao/index?type=";
        final String appkey = "&key=36ab48e6d2d6e065356fafd005f63acf";


        Thread thread = new Thread(new Runnable() {

            HttpURLConnection httpURLConnection = null;

            @Override
            public void run() {
                try {
                    httpURLConnection = (HttpURLConnection) new URL(urllink + type + appkey).openConnection();
                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setRequestMethod("GET");

                    final byte[] bytes = new byte[1024];

                    for (int length;
                         (length = httpURLConnection.getInputStream().read(bytes)) != -1;
                         back += new String(bytes, 0, length))
                        ;


                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (httpURLConnection.getInputStream() != null)
                            httpURLConnection.getInputStream().close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (httpURLConnection != null)
                            httpURLConnection.disconnect();
                    }
                }
            }
        });

        thread.start();

        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return back;

    }

}
